home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 3.iso / dist / fw_expect.idb / usr / freeware / bin / lpunlock.z / lpunlock
Text File  |  1999-01-26  |  3KB  |  96 lines

  1. #!/usr/freeware/bin/expect -f
  2.  
  3. # This script unhangs a printer which claims it is "waiting for lock".
  4. # Written by Don Libes.  Based on English instructions from Scott Paisley.
  5.  
  6. # lpunlock figures out if the printer is on a server, and if so which,
  7. # by looking in the local printcap file.  (You can override this by
  8. # supplying a server name as an additional argument.)  It then rlogins
  9. # to the server, recreates the device and resets the queue via lpc.
  10.  
  11. # assumes user has root privs on remote host via /.rhosts
  12.  
  13. # assumes printer is name of device on remote system
  14.  
  15. proc usage {} {
  16.     send_user "usage: lpunlock <printer> \[<server>\]\n"
  17.     send_user "example: lpunlock lw-isg durer\n"
  18.     exit
  19. }
  20.  
  21. if $argc==0 usage
  22. set printer [lindex $argv 0]
  23.  
  24. set client [exec hostname]
  25.  
  26. if {$argc == 1} {
  27.     # if no arg2, look in local printcap for info
  28.     spawn ed /etc/printcap
  29.     expect "\n"            ;# discard character count
  30.     send "/$printer/\r"
  31.     for {} 1 {} {
  32.         expect -re ".*:rm=(\[^:]*):.*\r\n" {
  33.             set server $expect_out(1,string)
  34.             break
  35.         } "\r\n*\\\r\n" {    ;# look at next line of entry
  36.             send "\r"
  37.         } "\r\n*\n" {        ;# no more lines of entry - give up
  38.             set server $client
  39.             break
  40.         }
  41.     }
  42. } else {
  43.     if {$argc == 2} {
  44.         set server [lindex $argv 1]
  45.     } else usage
  46. }
  47.  
  48. set whoami [exec whoami]
  49. if {[string match $server $client] && [string match $whoami "root"]} {
  50.     spawn csh
  51.     expect "# "
  52. } else {
  53.     # login to the print server as root.
  54.     # Set timeout high because login is slow.
  55.     set timeout 60
  56.     spawn rlogin $server -l root
  57.     expect    timeout    exit \
  58.         eof exit \
  59.         "Password*" {
  60.             send_user "\ncouldn't login to $server as root\n"
  61.             exit
  62.         } "1#*"
  63.     set timeout 10
  64. }
  65.  
  66. # run lpc and 'stop printer'
  67. send lpc\r                ; expect "lpc>*"
  68. send stop $printer\r            ; expect "unknown*" exit \
  69.                         "disabled*lpc>*"
  70.  
  71. # exit lpc and cd /dev
  72. send quit\r                ; expect "#*"
  73. send cd /dev\r                ; expect "#*"
  74.  
  75. # figure out major/minor device numbers
  76. send ls -l /dev/$printer\r        ; expect timeout {
  77.     send_user "\nbad device - couldn't get major/minor numbers\n"; exit
  78.                         } "crw*#*"
  79. scan $expect_out(buffer) "ls -l %*s %*s 1 root %d, %d" major minor
  80.  
  81. # delete the lock and the printer device itself
  82. send rm /var/spool/$printer/lock /dev/$printer\r    ; expect #*
  83.  
  84. # recreate the printer device
  85. send mknod $printer c $major $minor\r    ; expect #*
  86.  
  87. # run lpc and 'start printer'
  88. send lpc\r                ; expect lpc>*
  89. send start $printer\r            ; expect started*lpc>*
  90. send quit\r                ; expect #*
  91.  
  92. # logout
  93. send exit\r                ; expect eof
  94.  
  95. send_user Printer unlocked and restarted.\n
  96.